package com.yangrong.newsservice.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.yangrong.common.utils.PageUtils;
import com.yangrong.common.utils.R;
import com.yangrong.newsservice.entity.NewsEntity;
import com.yangrong.newsservice.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 新闻列表
 *
 * @author yangrong
 * @email 455833224@qq.com
 * @date 2020-08-26 15:21:07
 */
@RestController
@RequestMapping("article/service")
public class ServiceController {
    @Autowired
    private NewsService newsService;

    @RequestMapping("/list/allsize")
    public R listPage(@RequestParam Map<String, Object> params) {
        Integer size = newsService.getAllNewsSize(params);
        return R.ok().put("data", size);
    }

    /**
     * 加载更多
     *
     * @param params
     * @return
     */
    @RequestMapping("/list/more")
    public R listMore(@RequestParam Map<String, Object> params) {
        List<NewsEntity> data = newsService.getMore(params);
        return R.ok().put("data", data);
    }

    /**
     * 信息列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        List<NewsEntity> data = newsService.getNewsListByType(params);
        return R.ok().put("data", data);
    }

    /**
     * 信息种类列表
     */
    @RequestMapping("/list/type")
    public R listType() {
        List<String> listType = newsService.getListType();

        return R.ok().put("data", listType);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        NewsEntity entity = newsService.getById(id);

        return R.ok().put("wjList", entity);
    }

    /**
     * 获取文章内容
     *
     * @param params
     * @return
     */
    @RequestMapping("/articleContent")
    public R getContent(@RequestParam Map<String, Object> params) {
        String title = (String) params.get("title");
        String content = newsService.getContent(title);

        return R.ok().put("content", content);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody NewsEntity entity) {
        newsService.save(entity);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody NewsEntity entity) {
        newsService.updateById(entity);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids) {
        newsService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
